package fr.n7.sma.view;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.Scrollable;

import fr.n7.sma.model.Agent;
import fr.n7.sma.model.Cell;
import fr.n7.sma.model.GridModel;
import fr.n7.sma.model.listeners.GridModelListener;
import fr.n7.sma.model.listeners.events.GridModelEvent;
import fr.n7.sma.util.property.PropertyManager;
import fr.n7.sma.view.Grid.Properties;
import fr.n7.sma.view.model.AgentViewProperties;
import fr.n7.sma.view.model.CellViewProperties;

public class AgentsPanel extends JPanel implements Scrollable, GridModelListener<AgentViewProperties, CellViewProperties> {

	private static final long serialVersionUID = 1721758359849305619L;

	private JCheckBox continuousAgentLife;

	private Map<String, AgentPanel> agentPanels;
	
	private PropertyManager<Properties> propertyManager;

	public AgentsPanel(PropertyManager<Properties> propertyManager, GridModel<Cell<CellViewProperties>, AgentViewProperties, CellViewProperties> gridModel){
		super();
		this.propertyManager = propertyManager;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		continuousAgentLife = new JCheckBox("Raffraichir les vies des agents.");
		continuousAgentLife.addActionListener(new ContinuousAgentLifeListener(continuousAgentLife));
		this.add(continuousAgentLife);
		agentPanels = new HashMap<String , AgentPanel>();
		gridModel.addListener(this);
		for(Agent<AgentViewProperties, CellViewProperties> a : gridModel.getAgents()){
			addAgentPanel(a);
		}
	}

	private void addAgentPanel(Agent<AgentViewProperties, CellViewProperties> a){
		AgentPanel ap = new AgentPanel(a, propertyManager);
		ap.setContinuousLifeRefresh(continuousAgentLife.isSelected());
		agentPanels.put(a.getName(), ap);
		this.add(ap);
	}
	
	private class ContinuousAgentLifeListener implements ActionListener{

		private JCheckBox jCB;

		public ContinuousAgentLifeListener(JCheckBox jCB){
			this.jCB = jCB;
		}

		
		public void actionPerformed(ActionEvent ae) {
			for(AgentPanel ap : agentPanels.values()){
				ap.setContinuousLifeRefresh(jCB.isSelected());
			}
		}
	}

	
	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}

	
	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return 20;
	}

	
	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return 60;
	}

	
	public boolean getScrollableTracksViewportHeight() {
		return false;
	}

	
	public boolean getScrollableTracksViewportWidth() {
		return true;
	}

	
	public void gridModelChanged(
			GridModelEvent<AgentViewProperties, CellViewProperties> e) {
		switch(e.getEventType()){
		case AGENT_ADDED :{
			addAgentPanel(e.getAgent());
			break;
		}
		case AGENT_REMOVED :{
			remove(agentPanels.get(e.getAgent().getName()));
			break;
		}
		}
		
	}

}
